<?php


namespace App\Models;


use App\Services\CommonService;
use App\Services\GoodsService;
use Illuminate\Database\Eloquent\Model;

class AppletsIndexGoodsList extends Model
{
    protected $table = 'sp_index_goods_list';
    protected $primaryKey = 'id';
    public $timestamps = false;

    const ACTIVITY_NOT = 1;//不可推广
    const ACTIVITY_YES = 2; //可推广

    const STATUS_NOT_SHOW = 1;//下架推广
    const STATUS_YES_SHOW = 2;//上架推广

    const REDIS_LIST_KEY = 'index_list_key';//默认数据条数
    const CACHE_LIMIT_NUM = 8;//默认数据条数

    public static function getList($cateId,$page,$pageSize){
        $list = AppletsIndexGoodsList::from('sp_index_goods_list as list')
            ->select(['g.id as goods_id','g.title','g.imgurl','g.min_lease_term','g.min_price','g.rent_label'])
            ->leftjoin('sp_goods_2 as g', 'g.id', '=', 'list.goods_id')
            ->where([ 'g.is_delete'=>GoodsService::GOODS_NO_DELETE_STATUS, 'g.status'=>GoodsService::GOODS_STATUS_PUT,'list.list_cate_id'=>$cateId,'list.status'=>self::STATUS_YES_SHOW])
            ->orderBy('list.sort','asc')
            ->paginate($pageSize);
        $list = obToArray($list);
        if($list){
            foreach ($list['data'] as &$item){
                $item['is_huabei'] = 2;
                $buyout_price= GoodsSetmeal::where(['goods_id'=>$item['goods_id'],'is_delete'=>CommonService::GOODS_IS_DELETE_NOT])->value('buyout_price');
                if($item['min_price']&&$item['min_lease_term']&&$buyout_price){
                    $item['min_price'] = bcsub($item['min_price'],$buyout_price/$item['min_lease_term'],2);
                }
//                if($isHuabei){
//                    $item['is_huabei'] = 1;
//                }
            }
        }
        return $list;
    }
}
